package eproject.mm.com.projectsupervision.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import eproject.mm.com.projectsupervision.R;
import eproject.mm.com.projectsupervision.util.ImageScaleUtil;

public class PassengerAdapter extends RecyclerView.Adapter<PassengerAdapter.ViewHolder> {
    private Context mContext;
    private OnItemClickLitener listener;//点击事件接口
    private ArrayList<String> imageUrls;
    //private ImageFetcher imageFetcher;
    private ViewHolder viewHolder;
    private View view;

    private int itemHeight;

    /**
     * 在构造方法中传入图片地址的数据
     * @param context
     * @param imageUrls
     */
    public PassengerAdapter(Context context, ArrayList<String> imageUrls) {
        this.mContext = context;
        this.imageUrls = imageUrls;
        //初始化加载网络图片的jar包
        //imageFetcher = new ImageFetcher(context,100);
        //imageFetcher.setImageCache(ImageCache.getInstance(context,"temp"));
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        view = LayoutInflater.from(mContext).inflate(R.layout.lay_passager, null);
        viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        //设置内容为“hehe”的的元素为默认的添加按钮
        if (imageUrls.get(position).equals("hehe")) {
            //holder.imageViewBig.setBackgroundResource(R.drawable.add);
            holder.imageViewBig.setImageResource(R.drawable.add);
            //当图片是添加按钮的时候隐藏删除按钮
            holder.imageViewSmall.setVisibility(View.GONE);
        } else {
            holder.imageViewSmall.setVisibility(View.VISIBLE);
            /**
             * 判断图片路径是网络地址还是本地图片
             * 设置路径之中包含“storage”的为本地图片
             */
            if (imageUrls.get(position).contains("storage")) {
                try {
                    //File file = new File(imageUrls.get(position));
                    //将bitmap转化成drawable
                    //Bitmap bmp = MediaStore.Images.Media.getBitmap(mContext.getContentResolver(), Uri.fromFile(file));
                    //Drawable drawable =new BitmapDrawable(bmp);
                    //按比例扩大图片的size居中显示，使得图片长(宽)等于或大于View的长(宽)
                    //holder.imageViewBig.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    //holder.imageViewBig.setImageBitmap(bmp);

                    Bitmap imageBitmap = BitmapFactory.decodeFile(imageUrls.get(position));

                    Bitmap bm = BitmapFactory.decodeFile(imageUrls.get(position));

                    //Bitmap bmp=Bitmap.createScaledBitmap(bm, 100, bm.getHeight()*100/bm.getWidth(), true);
                    Bitmap bmp=ImageScaleUtil.centerSquareScaleBitmap(bm,100);
                    holder.imageViewBig.setImageBitmap(bmp);
                } catch (Exception e) {
                    e.printStackTrace();
                }
//                holder.imageViewBig.setImageURI(Uri.parse(imageUrls.get(position)));
            } else {
                //imageFetcher.loadImage(imageUrls.get(position), holder.imageViewBig);
            }
        }
    }


    @Override
    public int getItemCount() {
        return imageUrls.size();
    }



    public void setOnItemClickLitener(OnItemClickLitener listener) {
        this.listener = listener;
    }

    public interface OnItemClickLitener {
        void onBigClick(int position);

        void onSmallClick(int position);
    }
    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView imageViewBig, imageViewSmall;

        public ViewHolder(View itemView) {
            super(itemView);
            imageViewBig = (ImageView) itemView.findViewById(R.id.imageViewBig);
            imageViewSmall = (ImageView) itemView.findViewById(R.id.imageViewSmall);
            /**
             *
             * 因为元素是变化的，动态的，所以对点击事件的处理放在ViewHolder类里面，调用getposition()
             * 可以获取到当前的元素位子
             */
            imageViewBig.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //int position = (Integer) v.getTag();
                    listener.onBigClick(getPosition());
                }
            });
            imageViewSmall.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //int position = (Integer) v.getTag();
                    listener.onSmallClick(getPosition());
                }
            });

            itemView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
                    itemHeight = view.getMeasuredHeight();
                    return true;
                }
            });
        }
    }

    public int getItemHeight(){
        return itemHeight;
    }
}